
# $Id: Instance.pm 96 2013-03-25 07:47:45Z chengxin.cai $

use strict;

package Provision::Instance;

use Provision::Object;
use base 'Provision::Object';

use config ':all';
use instance ':all';

use Provision::User;

my $script_loc = $nvpath . '/instance';

my @provisionSteps = (
    {
      title => 'Check Exar card',
      cmd   => $script_loc . '/exarconf',
    },
    {
      title => 'Initialize msa',
      cmd   => $script_loc . '/instance_msaconfig',
    },
    {
      title => 'Dump msainfo',
      cmd   => $script_loc . '/dumpmsainfo',
    },
    {
      title => 'Lun config',
      cmd   => $script_loc . '/lunconfig',
    },
    {
      title => 'Create users',
      code => sub {
          my ($inst_id, $test) = @_;
          my @userlist = get_instance_user_list($inst_id);

          for my $squser ( @userlist ) {
              my $user = Provision::User->new($squser);

              my $ret = $user->create($test);
              return $ret if ($ret != 0);
          }
          return config::OK;
      }
    },
    {
      title => 'Map seaquest storage',
      cmd   => $script_loc . '/mapsqstorage',
    },
    {
      title => 'Mount seaquest database',
      cmd   => $script_loc . '/dbmounts',
    },
    {
      title => 'Set database permission',
      cmd   => $script_loc . '/set_dbperms',
    },
);

my @undoStep = (
    {
      title => 'Lun config undo',
      cmd   => $script_loc . '/lunconfig',
      args  => '--undo',

    },
    {
      title => 'Remove users',
      code  => sub {
          my ($inst_id, $test) = @_;
          my @userlist = get_instance_user_list($inst_id);

          for my $squser ( @userlist ) {
              my $user = Provision::User->new($squser);

              my $ret = $user->delete($test);
              return $ret if ($ret != 0);
          }
          return config::OK;
      }
    },
);

sub new {
    my ($proto, $id) = @_;
    my $class = ref $proto || $proto;

    my $self = $class->SUPER::new($id);

    $self->{squser} = get_instance_defaultuser($id);

    return $self;
}

sub get_idarg { my $self = shift; return '--squser=' . $self->{squser}; }

sub create {
    my ($self, $test) = @_;

    return $self->SUPER::create( \@provisionSteps, $test );
}

sub delete {
    my ($self, $test) = @_;

    return $self->SUPER::delete( \@undoSteps, $test );

}

1;

